import 'package:flutter/material.dart';
import 'package:shimmer/shimmer.dart';

class HomeSkeleton extends StatelessWidget {
  const HomeSkeleton({super.key});

  Widget _shimmerBox({
    double height = 16,
    double width = double.infinity,
    BorderRadius? radius,
  }) {
    return Shimmer.fromColors(
      baseColor: Colors.grey.shade300,
      highlightColor: Colors.grey.shade100,
      child: Container(
        height: height,
        width: width,
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: radius ?? BorderRadius.circular(8),
        ),
      ),
    );
  }

  Widget _shimmerGrid({int count = 4, double size = 80}) {
    return Wrap(
      spacing: 12,
      runSpacing: 12,
      children: List.generate(count, (_) => _shimmerBox(height: size, width: size)),
    );
  }

  Widget _shimmerStatRow() {
    return Row(
      children: List.generate(3, (i) => Expanded(
        child: Padding(
          padding: EdgeInsets.only(right: i < 2 ? 8 : 0),
          child: _shimmerBox(height: 100),
        ),
      )),
    );
  }

  Widget _shimmerBanner() => _shimmerBox(height: 120);

  Widget _shimmerSearchBar() => _shimmerBox(height: 36, width: 160);

  Widget _shimmerCategoryList({int count = 4}) {
    return Column(
      children: List.generate(count, (_) => Padding(
        padding: const EdgeInsets.symmetric(vertical: 6),
        child: _shimmerBox(height: 60),
      )),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: const BoxDecoration(
        image: DecorationImage(
          image: AssetImage("assets/home/background.jpg"),
          fit: BoxFit.cover,
        ),
      ),
      child: ListView(
        padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 8),
        children: [
          _shimmerGrid(),
          const SizedBox(height: 24),
          _shimmerBox(height: 20, width: 120), // 我的收藏标题
          const SizedBox(height: 12),
          _shimmerGrid(),
          const SizedBox(height: 24),
          _shimmerStatRow(),
          const SizedBox(height: 26),
          _shimmerBanner(),
          const SizedBox(height: 16),
          _shimmerBox(height: 20, width: 120), // 分类标题
          const SizedBox(height: 12),
          _shimmerSearchBar(),
          const SizedBox(height: 16),
          _shimmerCategoryList(),
        ],
      ),
    );
  }
}
